package com.kg.sdk.dlg;

import android.app.Activity;
import android.app.Dialog;
import android.content.Intent;
import android.net.Uri;
import android.provider.Settings;
import android.support.annotation.NonNull;
import android.view.Display;
import android.view.LayoutInflater;
import android.view.View;
import android.view.WindowManager;
import android.widget.TextView;
import com.kg.sdk.KPGameSdk;
import com.kg.sdk.util.CommonUtil;
import com.kg.sdk.util.Helper;

public class KPPermissionDlg extends Dialog implements View.OnClickListener {
    private TextView btnCancel;
    private TextView btnOk;
    private TextView tvTip;
    private Activity act;
    public KPPermissionDlg(@NonNull Activity context) {
        super(context, Helper.getStyleResId(context,"DGDialog_NoTitle_Transparent"));
        act = context;
        initView();
    }
    protected void initDialog(){
        WindowManager windowManager = this.getWindow().getWindowManager();
        Display display = windowManager.getDefaultDisplay();
        WindowManager.LayoutParams lp = getWindow().getAttributes();
        lp.width = (int)(display.getWidth() * 0.8); //设置宽度
        lp.height =  WindowManager.LayoutParams.WRAP_CONTENT ;
        getWindow().setAttributes(lp);
    }
    protected void initView(){
       View  mMainView = getMainView();
        setContentView(mMainView);
    }
    protected View getMainView() {
        int layoutId = Helper.getLayoutResId(getContext(),"dlg_kp_permission");
        View view = LayoutInflater.from(getContext()).inflate(layoutId,null);
        tvTip = (TextView)view.findViewById(Helper.getIdResId(getContext(),"tvTip"));
        btnCancel = (TextView) view.findViewById(Helper.getIdResId(getContext(),"btnCancel"));
        btnOk =  (TextView)view.findViewById(Helper.getIdResId(getContext(),"btnOk"));
        btnCancel.setOnClickListener(this);
        btnOk.setOnClickListener(this);

        return view;
    }

    public void setTip(String tip){
        tvTip.setText(tip);
    }

    @Override
    public void onClick(View v) {
        int id = v.getId();
        if(id == Helper.getIdResId(getContext(),"btnOk")){
            onOkClick();
        }
        dismiss();
    }

    public void onOkClick(){
        try{
            Intent intent=new Intent(Settings.ACTION_MANAGE_OVERLAY_PERMISSION);
            intent.setData(Uri.fromParts("package", getContext().getPackageName(), null));
            act.startActivityForResult(intent, KPGameSdk.CHECKPRESSMISSON_CODE);
        }catch (Exception e)
        {
            e.printStackTrace();
        }
        CommonUtil.showToast(getContext(),"请在权限管理中开启显示悬浮窗的权限");
    }

    public void showDialog(){
        if(!isShowing()){
            show();
        }
    }

}
